package dhm.com.hpx.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.yhy.gvp.listener.OnItemClickListener;
import com.yhy.gvp.widget.GridViewPager;
import com.youth.banner.Banner;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import dhm.com.hpx.R;
import dhm.com.hpx.activity.AllsourceActivity;
import dhm.com.hpx.activity.ArticleActivity;
import dhm.com.hpx.activity.ExtensionActivity;
import dhm.com.hpx.activity.IndustryActivity;
import dhm.com.hpx.activity.LoginActivity;
import dhm.com.hpx.activity.ManagerActivity;
import dhm.com.hpx.activity.MapsActivity;
import dhm.com.hpx.activity.MyrecommendActivity;
import dhm.com.hpx.activity.PosterActivity;
import dhm.com.hpx.activity.SearchActivity;
import dhm.com.hpx.activity.VideoActivity;
import dhm.com.hpx.activity.VisitorActivity;
import dhm.com.hpx.adapter.IndexTypeAdapter;
import dhm.com.hpx.adapter.MainArticleAdapter;
import dhm.com.hpx.adapter.MainPosterAdapter;
import dhm.com.hpx.adapter.MainVideoAdapter;
import dhm.com.hpx.base.BaseFragment;
import dhm.com.hpx.base.MainBanner;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.GetNum;
import dhm.com.hpx.entity.GetUserInfoBean;
import dhm.com.hpx.entity.IndexAllTypeBean;
import dhm.com.hpx.entity.MainArticleBean;
import dhm.com.hpx.entity.MainGonggao;
import dhm.com.hpx.entity.MainPosterBean;
import dhm.com.hpx.entity.MainVideoBean;
import dhm.com.hpx.utils.GlideUtil;
import dhm.com.hpx.utils.SpUtils;
import dhm.com.hpx.utils.mapapi.Util;

public class MainFragment extends BaseFragment implements LoginContract.IView {
    Unbinder unbinder;
    @BindView(R.id.banner)
    Banner home_banner;
    @BindView(R.id.line3)
    TextView line3;
    @BindView(R.id.main_num)
    TextView main_num;
    @BindView(R.id.recy)
    RecyclerView recy;
    @BindView(R.id.home_topnews)
    ViewFlipper viewFlipper;
    @BindView(R.id.article_text)
    TextView articleText;
    @BindView(R.id.article_line)
    TextView articleLine;
    @BindView(R.id.video_text)
    TextView videoText;
    @BindView(R.id.video_line)
    TextView videoLine;
    @BindView(R.id.poster_text)
    TextView posterText;
    @BindView(R.id.poster_line)
    TextView posterLine;
    @BindView(R.id.grid_viewpager)
    GridViewPager gridViewpager;
    @BindView(R.id.indicator_container)
    MagicIndicator indicatorContainer;

    private PressenterImpl pressenter;

    List<IndexAllTypeBean.TypeListBean> typeDatas;
    private String userType;
    private String uid;

    @Override
    protected int getLayout() {
        return R.layout.fragment_main;
    }

    IndexTypeAdapter indexTypeAdapter;

    @Override
    protected void initView() {
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        api = WXAPIFactory.createWXAPI(getActivity(), Constant.Wx_APP_ID, false);

        userType = SpUtils.getString(getActivity(), "userType");

        uid = SpUtils.getString(getActivity(), "uid");

        pressenter.sendMessage(getActivity(), Constant.get_ads, new HashMap<String, String>(), MainBanner.class);

        typeDatas = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            IndexAllTypeBean.TypeListBean bean = new IndexAllTypeBean.TypeListBean();
            switch (i) {
                case 0:
                    bean.setImg(R.mipmap.main_one);
                    bean.setName("全图客源");
                    break;
                case 1:
                    bean.setImg(R.mipmap.main_two);
                    bean.setName("分类客源");
                    break;
                case 2:
                    bean.setImg(R.mipmap.main_three);
                    bean.setName("范围客源");
                    break;
                case 3:
                    bean.setImg(R.mipmap.main_four);
                    bean.setName("全网客源");
                    break;
                case 4:
                    bean.setImg(R.mipmap.main_five);
                    bean.setName("客源管理");
                    break;
                case 5:
                    bean.setImg(R.mipmap.main_six);
                    bean.setName("获客视频");
                    break;
                case 6:
                    bean.setImg(R.mipmap.main_seven);
                    bean.setName("获客文章");
                    break;
                case 7:
                    bean.setImg(R.mipmap.main_eight);
                    bean.setName("视频推广");
                    break;
                case 8:
                    bean.setImg(R.mipmap.main_nine);
                    bean.setName("文章推广");
                    break;
                case 9:
                    bean.setImg(R.mipmap.main_ten);
                    bean.setName("我的访客");
                    break;
                default:
//                    bean.setImg(R.mipmap.main_four);
//                    bean.setName("我的访客");
                    break;
            }
            typeDatas.add(bean);
        }


        indexTypeAdapter = new IndexTypeAdapter(getActivity(), R.layout.item_index_type, typeDatas);
        gridViewpager.setGVPAdapter(indexTypeAdapter);
        indexTypeAdapter.setOnItemClickListener(new OnItemClickListener<IndexAllTypeBean.TypeListBean>() {
            @Override
            public void onItemClick(View view, int position, IndexAllTypeBean.TypeListBean data) {
                Intent intent;
                switch (position) {
                    case 0:
                        intent = new Intent(getActivity(), MapsActivity.class);
                        intent.putExtra("type", "1");
                        getActivity().startActivity(intent);
                        break;
                    case 1:
                        intent = new Intent(getActivity(), MapsActivity.class);
                        intent.putExtra("type", "2");
                        getActivity().startActivity(intent);
                        break;
                    case 2:
                        intent = new Intent(getActivity(), MapsActivity.class);
                        intent.putExtra("type", "3");
                        getActivity().startActivity(intent);
                        break;
                    case 3:
                        intent = new Intent(getActivity(), AllsourceActivity.class);
                        getActivity().startActivity(intent);
                        break;
                    case 4:
                        intent = new Intent(getActivity(), ManagerActivity.class);
                        intent.putExtra("type", "1");
                        getActivity().startActivity(intent);
                        break;
                    case 5:
                        intent = new Intent(getActivity(), IndustryActivity.class);
                        intent.putExtra("type", "2");
                        getActivity().startActivity(intent);
                        break;
                    case 6:
                        intent = new Intent(getActivity(), IndustryActivity.class);
                        intent.putExtra("type", "1");
                        getActivity().startActivity(intent);
                        break;
                    case 7:
                        switch (userType) {
                            case "0":
                                Toast.makeText(getActivity(), "暂无权限查看，请升级会员", Toast.LENGTH_SHORT).show();
                                break;
                            case "1":
                            case "2":
                                Toast.makeText(getActivity(), "暂无权限查看，请升级年卡会员", Toast.LENGTH_SHORT).show();
                                break;
                            case "4":
                                Toast.makeText(getActivity(), "您的会员已过期", Toast.LENGTH_SHORT).show();
                                break;
                            case "3":
                            default:
                                intent = new Intent(getActivity(), ExtensionActivity.class);
                                intent.putExtra("type", "2");
                                startActivity(intent);
                        }


                        break;
                    case 8:

                        switch (userType) {
                            case "0":
                                Toast.makeText(getActivity(), "暂无权限查看，请升级会员", Toast.LENGTH_SHORT).show();
                                break;
                            case "1":
                            case "2":
                                Toast.makeText(getActivity(), "暂无权限查看，请升级年卡会员", Toast.LENGTH_SHORT).show();
                                break;
                            case "4":
                                Toast.makeText(getActivity(), "您的会员已过期", Toast.LENGTH_SHORT).show();
                                break;
                            case "3":
                            default:
                                intent = new Intent(getActivity(), ExtensionActivity.class);
                                intent.putExtra("type", "1");
                                startActivity(intent);
                        }


                        break;
                    case 9:

                        switch (userType) {
                            case "0":
                                Toast.makeText(getActivity(), "暂无权限查看，请升级会员", Toast.LENGTH_SHORT).show();
                                break;
                            case "4":
                                Toast.makeText(getActivity(), "您的会员已过期", Toast.LENGTH_SHORT).show();
                                break;
                            case "1":
                            case "2":
                            case "3":
                            default:
                                intent = new Intent(getActivity(), VisitorActivity.class);
                                startActivity(intent);
                        }

                        break;
                    default:
                }
            }
        });


        CommonNavigator commonNavigator = new CommonNavigator(getActivity());//指示器
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                int num = typeDatas.size() / 10;
                if (typeDatas.size() % 10 > 0) {
                    num++;
                }
                return typeDatas == null ? 0 : num;
            }

            @Override
            public IPagerTitleView getTitleView(Context mContext, final int i) {
                CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(getActivity());
                View view = View.inflate(getActivity(), R.layout.main_index, null);
                final ImageView iv_image = view.findViewById(R.id.iv_image);
                iv_image.setImageResource(R.drawable.point4);

                commonPagerTitleView.setContentView(view);//指示器引入外部布局，可知指示器内容可根据需求设置，多样化
                commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {
                    @Override
                    public void onSelected(int i, int i1) {
                        iv_image.setImageResource(R.drawable.back_regists);
                    }

                    @Override
                    public void onDeselected(int i, int i1) {
                        iv_image.setImageResource(R.drawable.back_gray);
                    }

                    @Override
                    public void onLeave(int i, int i1, float v, boolean b) {

                    }

                    @Override
                    public void onEnter(int i, int i1, float v, boolean b) {

                    }
                });
                return commonPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        });
        indicatorContainer.setNavigator(commonNavigator);
        ViewPagerHelper.bind(indicatorContainer, gridViewpager);//页面内容与指示器关联


        recy.setHasFixedSize(true);
        recy.setNestedScrollingEnabled(false);
        initData();
    }

    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_main, container, false);
        }
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    protected void initData() {

        Map<String,String> map=new HashMap<>();
        map.put("token",Constant.TOKEN);
        map.put("user_id",uid);
        pressenter.sendMessage(getActivity(),Constant.visitor_news,map, GetNum.class);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private IWXAPI api;

    @OnClick({R.id.search,R.id.one, R.id.two, R.id.three, R.id.four, R.id.five, R.id.six, R.id.seven, R.id.eight, R.id.nine, R.id.ten, R.id.article, R.id.video, R.id.poster})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.search:
                intent = new Intent(getActivity(), SearchActivity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.one:
                intent = new Intent(getActivity(), MapsActivity.class);
                intent.putExtra("type", "1");
                getActivity().startActivity(intent);
                break;
            case R.id.two:
                intent = new Intent(getActivity(), MapsActivity.class);
                intent.putExtra("type", "2");
                getActivity().startActivity(intent);
                break;
            case R.id.three:
                intent = new Intent(getActivity(), MapsActivity.class);
                intent.putExtra("type", "3");
                getActivity().startActivity(intent);
                break;
            case R.id.four:
                intent = new Intent(getActivity(), AllsourceActivity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.five:
                intent = new Intent(getActivity(), ManagerActivity.class);
                intent.putExtra("type", "1");
                getActivity().startActivity(intent);
                break;
            case R.id.six:
                intent = new Intent(getActivity(), IndustryActivity.class);
                intent.putExtra("type", "2");
                getActivity().startActivity(intent);
                break;
            case R.id.seven:
                intent = new Intent(getActivity(), IndustryActivity.class);
                intent.putExtra("type", "1");
                getActivity().startActivity(intent);
                break;
            case R.id.eight:

                switch (userType) {
                    case "0":
                        Toast.makeText(getActivity(), "暂无权限查看，请升级会员", Toast.LENGTH_SHORT).show();
                        break;
                    case "1":
                    case "2":
                        Toast.makeText(getActivity(), "暂无权限查看，请升级年卡会员", Toast.LENGTH_SHORT).show();
                        break;
                    case "4":
                        Toast.makeText(getActivity(), "您的会员已过期", Toast.LENGTH_SHORT).show();
                        break;
                    case "3":
                    default:
                        intent = new Intent(getActivity(), ExtensionActivity.class);
                        intent.putExtra("type", "2");
                        startActivity(intent);
                }

                break;
            case R.id.nine:
                switch (userType) {
                    case "0":
                        Toast.makeText(getActivity(), "暂无权限查看，请升级会员", Toast.LENGTH_SHORT).show();
                        break;
                    case "1":
                    case "2":
                        Toast.makeText(getActivity(), "暂无权限查看，请升级年卡会员", Toast.LENGTH_SHORT).show();
                        break;
                    case "4":
                        Toast.makeText(getActivity(), "您的会员已过期", Toast.LENGTH_SHORT).show();
                        break;
                    case "3":
                    default:
                        intent = new Intent(getActivity(), ExtensionActivity.class);
                        intent.putExtra("type", "1");
                        startActivity(intent);
                }

                break;
            case R.id.ten:
                switch (userType) {
                    case "0":
                        Toast.makeText(getActivity(), "暂无权限查看，请升级会员", Toast.LENGTH_SHORT).show();
                        break;
                    case "4":
                        Toast.makeText(getActivity(), "您的会员已过期", Toast.LENGTH_SHORT).show();
                        break;
                    case "1":
                    case "2":
                    case "3":
                    default:
                        intent = new Intent(getActivity(), VisitorActivity.class);
                        startActivity(intent);
                }

                break;
            case R.id.article:
                pressenter.sendMessage(getActivity(), Constant.elite_article_list, new HashMap<String, String>(), MainArticleBean.class);
                break;
            case R.id.video:
                pressenter.sendMessage(getActivity(), Constant.elite_video_list, new HashMap<String, String>(), MainVideoBean.class);
                break;
            case R.id.poster:
                pressenter.sendMessage(getActivity(), Constant.elite_poster_list, new HashMap<String, String>(), MainPosterBean.class);

                break;
            default:
        }
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    @Override
    public void onStop() {
        super.onStop();
        viewFlipper.stopFlipping();
    }



    @Override
    public void requesta(Object data) {
        if (data instanceof MainBanner) {
            MainBanner banner = (MainBanner) data;

            if (banner.getCode() == 1) {
                home_banner.setImages(banner.getData().getInfo())
                        .setDelayTime(3000)
                        .isAutoPlay(true)
                        .setBannerAnimation(Transformer.DepthPage)
                        .setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(int position) {
                             /*   //初始化一个WXWebpageObject，填写url
                                WXWebpageObject webpage = new WXWebpageObject();
                                webpage.webpageUrl = "https://www.pgyer.com/OAPF";

//用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
                                WXMediaMessage msg = new WXMediaMessage(webpage);
                                msg.title = "欢迎下载趣企客app";
                                msg.description = "趣企客让您获得海量客源";
                                Bitmap thumbBmp = BitmapFactory.decodeResource(getResources(), R.mipmap.main_yao);
                                msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

//构造一个Req
                                SendMessageToWX.Req req = new SendMessageToWX.Req();
                                req.transaction = buildTransaction("webpage");
                                req.message = msg;
                                req.scene = SendMessageToWX.Req.WXSceneSession;  // 目前只支持会话
//调用api接口，发送数据到微信
                                api.sendReq(req);
*/
/*

                                //初始化一个WXWebpageObject，填写url
                                WXWebpageObject webpage = new WXWebpageObject();
                                webpage.webpageUrl ="https://www.pgyer.com/OAPF";

//用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
                                WXMediaMessage msg = new WXMediaMessage(webpage);
                                msg.title ="欢迎下载趣企客app ";
                                msg.description ="趣企客让您获得海量客源";
                                Bitmap thumbBmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_music_thumb);
                                msg.thumbData =Util.bmpToByteArray(thumbBmp, true);

//构造一个Req
                                SendMessageToWX.Req req = new SendMessageToWX.Req();
                                req.transaction = buildTransaction("webpage");
                                req.message =msg;
                                req.scene =SendMessageToWX.Req.WXSceneSession;
//                                req.userOpenId = getOpenId();

//调用api接口，发送数据到微信
                                api.sendReq(req);
*/


                                //初始化一个WXWebpageObject，填写url
                                WXWebpageObject webpage = new WXWebpageObject();
//                webpage.webpageUrl = "http://www.quqike.cn/appapi/appletscode/getwxacode/uid/" + uid + "/type/1/article_id/" + article_id;

                                webpage.webpageUrl = "https://www.pgyer.com/_hpx";

//用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
                                WXMediaMessage msg = new WXMediaMessage(webpage);
                                msg.title = "欢迎下载采客宝app";
                                msg.description = "采客宝让您获得海量客源";

                                // 小程序消息desc
                                Bitmap thumbBmp = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
                                msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

//构造一个Req
                                SendMessageToWX.Req req = new SendMessageToWX.Req();
                                req.transaction = buildTransaction("webpage");
                                req.message = msg;
                                req.scene = SendMessageToWX.Req.WXSceneSession;  // 目前只支持会话
//调用api接口，发送数据到微信
                                api.sendReq(req);


                            }
                        })
                        .setImageLoader(new ImageLoader() {
                            @Override
                            public void displayImage(Context context, Object path, ImageView imageView) {
                                MainBanner.DataBean.InfoBean images = (MainBanner.DataBean.InfoBean) path;
                                GlideUtil.loadImage(context, "/" + images.getContent(), imageView);
                            }
                        }).start();

            } else {
                Toast.makeText(getActivity(), banner.getMessage(), Toast.LENGTH_SHORT).show();
            }

            pressenter.sendMessage(getActivity(), Constant.elite_article_list, new HashMap<String, String>(), MainArticleBean.class);
        } else if (data instanceof MainArticleBean) {
            final MainArticleBean mainArticleBean = (MainArticleBean) data;
            if (mainArticleBean.getCode() == 1) {
                recy.setVisibility(View.VISIBLE);
                videoText.setTextColor(Color.parseColor("#666666"));
                posterText.setTextColor(Color.parseColor("#666666"));
                videoLine.setVisibility(View.INVISIBLE);
                posterLine.setVisibility(View.INVISIBLE);
                articleText.setTextColor(getResources().getColor(R.color.main));
                articleLine.setVisibility(View.VISIBLE);


                LinearLayoutManager manager = new LinearLayoutManager(getActivity());
                manager.setOrientation(LinearLayoutManager.VERTICAL);

                recy.setLayoutManager(manager);

                MainArticleAdapter mainArticleAdapter = new MainArticleAdapter(getActivity());

                mainArticleAdapter.setdeleteClick(new MainArticleAdapter.OnClick() {
                    @Override
                    public void item(int posttion) {
                        Intent intent = new Intent(getActivity(), ArticleActivity.class);
                        intent.putExtra("article_id", mainArticleBean.getData().get(posttion).getId() + "");
                        intent.putExtra("coverImg", mainArticleBean.getData().get(posttion).getCoverImg() + "");
                        getActivity().startActivity(intent);

                    }
                });

                recy.setAdapter(mainArticleAdapter);


                mainArticleAdapter.setList(mainArticleBean.getData());


            } else {
                recy.setVisibility(View.GONE);

            }


            pressenter.sendMessage(getActivity(), Constant.index_notice, new HashMap<String, String>(), MainGonggao.class);


        } else if (data instanceof MainGonggao) {
            MainGonggao mainGonggao = (MainGonggao) data;
            if (mainGonggao.getCode() == 1) {

                for (MainGonggao.DataBean mainGonggaos : mainGonggao.getData()) {
                    View view = LayoutInflater.from(getActivity()).inflate(R.layout.hometopnewsitem, viewFlipper, false);
                    TextView title = view.findViewById(R.id.zixuntitle);
                    title.setText(mainGonggaos.getPost_title());
                    viewFlipper.addView(view);
                    viewFlipper.startFlipping();
                }
            }

            Map<String, String> map = new HashMap<>();
            map.put("token", Constant.TOKEN);
            map.put("uid", uid);
            pressenter.sendMessage(getActivity(), Constant.get_userinfo, map, GetUserInfoBean.class);


        } else if (data instanceof GetUserInfoBean) {
            GetUserInfoBean getUserInfoBean = (GetUserInfoBean) data;

            if (getUserInfoBean.getCode() == 1) {
                userType = "0";
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String date = sdf.format(new java.util.Date());
                if (getUserInfoBean.getData().getUserType() > 0) {
                    if (getUserInfoBean.getData().getUserType() == 1) {
                        userType = "1";
                    } else if (getUserInfoBean.getData().getUserType() == 2) {
                        userType = "2";
                    } else if (getUserInfoBean.getData().getUserType() == 3) {
                        userType = "3";
                    }
                    if(!getUserInfoBean.getData().getUser_validity_time().equals("0")){
                        timeCompare(getUserInfoBean.getData().getUser_validity_time(), date);
                    }

                }

                if (userType.equals("0")||userType.equals("4")){
                    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    try
                    {
                        Date d1 = df.parse(date);
                        Date d2 = df.parse(getUserInfoBean.getData().getLast_login_time());
                        long diff = d1.getTime() - d2.getTime();//这样得到的差值是毫秒级别
                        long days = diff / (1000 * 60 * 60 * 24);

//                        long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
//                        long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);

                        if (days>=2){
                            SpUtils.clear(getActivity());
                            Intent intent = new Intent(getActivity(), LoginActivity.class);
                            getActivity().startActivity(intent);
                            getActivity().finish();
                            Toast.makeText(getActivity(), "登录已过期，请重新登陆", Toast.LENGTH_SHORT).show();
                        }

                    }catch (Exception e) {

                    }
                }

                SpUtils.saveString(getActivity(),"userType",getUserInfoBean.getData().getUserType()+"");
            }

        }else if (data instanceof GetNum){
            GetNum visitorBean= (GetNum) data;
            if (visitorBean.getCode()==1){
                if (visitorBean.getData()>0){
                    main_num.setVisibility(View.VISIBLE);
                    main_num.setText(visitorBean.getData()+"");
                }else {
                    main_num.setVisibility(View.GONE);
                    main_num.setText("0");
                }
            }
        } else if (data instanceof MainVideoBean) {
            final MainVideoBean mainVideoBean = (MainVideoBean) data;
            if (mainVideoBean.getCode() == 1) {
                recy.setVisibility(View.VISIBLE);
                posterText.setTextColor(Color.parseColor("#666666"));
                articleText.setTextColor(Color.parseColor("#666666"));
                posterLine.setVisibility(View.INVISIBLE);
                articleLine.setVisibility(View.INVISIBLE);
                videoText.setTextColor(getResources().getColor(R.color.main));
                videoLine.setVisibility(View.VISIBLE);

                GridLayoutManager manager = new GridLayoutManager(getActivity(),2);
                manager.setOrientation(LinearLayoutManager.VERTICAL);

                recy.setLayoutManager(manager);

                MainVideoAdapter mainVideoAdapter = new MainVideoAdapter(getActivity());
                mainVideoAdapter.setdeleteClick(new MainVideoAdapter.OnClick() {
                    @Override
                    public void item(int posttion) {
                        Intent intent = new Intent(getActivity(), VideoActivity.class);
                        intent.putExtra("video_id", mainVideoBean.getData().get(posttion).getId() + "");
                        intent.putExtra("coverImg", mainVideoBean.getData().get(posttion).getCoverImg() + "");
                        getActivity().startActivity(intent);
                    }
                });
                recy.setAdapter(mainVideoAdapter);


                mainVideoAdapter.setList(mainVideoBean.getData());

            } else {
                recy.setVisibility(View.GONE);
                Toast.makeText(getActivity(), mainVideoBean.getMessage(), Toast.LENGTH_SHORT).show();
            }


        } else if (data instanceof MainPosterBean) {
            final MainPosterBean mainPosterBean = (MainPosterBean) data;
            if (mainPosterBean.getCode() == 1) {
                recy.setVisibility(View.VISIBLE);
                videoText.setTextColor(Color.parseColor("#666666"));
                articleText.setTextColor(Color.parseColor("#666666"));
                videoLine.setVisibility(View.INVISIBLE);
                articleLine.setVisibility(View.INVISIBLE);
                posterText.setTextColor(getResources().getColor(R.color.main));
                posterLine.setVisibility(View.VISIBLE);


                LinearLayoutManager manager = new LinearLayoutManager(getActivity());
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                recy.setLayoutManager(manager);

                MainPosterAdapter mainPosterAdapter = new MainPosterAdapter(getActivity());

                mainPosterAdapter.setdeleteClick(new MainPosterAdapter.OnClick() {
                    @Override
                    public void item(int posttion) {
                        Intent intent = new Intent(getActivity(), PosterActivity.class);
                        intent.putExtra("poster_id", mainPosterBean.getData().get(posttion).getId() + "");
                        getActivity().startActivity(intent);
                    }
                });

                recy.setAdapter(mainPosterAdapter);

                mainPosterAdapter.setList(mainPosterBean.getData());

            } else {
                recy.setVisibility(View.GONE);
                Toast.makeText(getActivity(), mainPosterBean.getMessage(), Toast.LENGTH_SHORT).show();
            }


        }

    }

    @Override
    public void fail(String error) {

    }


    /**
     * 判断2个时间大小
     * yyyy-MM-dd HH:mm 格式（自己可以修改成想要的时间格式）
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public String timeCompare(String startTime, String endTime) {
        int i = 0;
        //注意：传过来的时间格式必须要和这里填入的时间格式相同
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date1 = dateFormat.parse(startTime);//开始时间
            Date date2 = dateFormat.parse(endTime);//结束时间
            // 1 结束时间小于开始时间 2 开始时间与结束时间相同 3 结束时间大于开始时间
            if (date2.getTime() < date1.getTime()) {
                //没过期
                i = 1;
            } else if (date2.getTime() == date1.getTime()) {
                //过期
                userType = "4";
                i = 2;
            } else if (date2.getTime() > date1.getTime()) {
                //过期
                userType = "4";
                i = 3;
            }
        } catch (Exception e) {

        }

        return i + "";
    }


}
